<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="mainwidth" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

          <!--  时间类型-->
          <el-form-item label="时间类型" prop="status">
            <el-select
              v-model="queryParams.timetype"
              placeholder="时间类型"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in timetypeInstall"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="selTime"
              type="daterange"
              :picker-options="pickerOptions"
              range-separator="至"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 360px"
              align="right">
            </el-date-picker>

          </el-form-item>

        </el-form>

        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" style="margin-bottom: 10px">
          <el-row :gutter="10" class="mb8" style="margin-bottom:-10px;line-height:35px">
            <el-col :span="1.5">
              <el-input
                v-model="queryParams.value"
                placeholder="查询值"
                clearable
                size="small"
                style="width: 350px"
                @keyup.enter.native="handleQuery"
              >
                <el-select
                  v-model="queryParams.type"
                  placeholder="查询条件"
                  clearable
                  slot="prepend"
                  style="width: 110px"
                >
                  <el-option
                    v-for="dict in typePlaceFloor"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-input>
            </el-col>

            <el-col :span="1.5">
              <el-form-item label="建筑物名称" prop="status" label-width="100px" style="margin-bottom:10px;line-height:35px">
                <el-select
                  v-model="queryParams.building_name"
                  placeholder="建筑物名称"
                  clearable
                  size="small"
                  style="width: 150px"
                >
                  <el-option
                    v-for="dict in Name_result"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"

              >新增
              </el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </el-form>

        <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">

          <el-table-column
            align="center"
            label="序号"
            width="70px">
            <template slot-scope="scopes">
              {{ scopes.$index+1 }}
            </template>
          </el-table-column>

<!--          <el-table-column label="序号" align="center" prop="id" v-if="columns[0].visible"/>-->
          <el-table-column label="楼层" align="center" prop="y_floor" v-if="columns[1].visible"/>
          <el-table-column label="安装点数量" align="center" prop="Installation_points" v-if="columns[2].visible">
            <template slot-scope="scope">
              <router-link :to="'/installpoint/installpoint/index'+ scope.row.building_name" class="link-type">
                <el-button type="info" size="maini" circle>
                  {{ scope.row.cOunt}}
                </el-button>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="建筑物名称" align="center" prop="building_name" v-if="columns[3].visible">
            <template slot-scope="scope">
              {{ tools.getDkeyValue(Name_result, scope.row.building_name) }}
            </template>
          </el-table-column>
          <el-table-column label="平面图" align="center" prop="plane_figure" v-if="columns[4].visible"/>
          <el-table-column label="创建时间" align="center" prop="create_time" v-if="columns[5].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.create_time) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 新增楼层 -->
    <el-dialog title="新增楼层" :visible.sync="open" width="30%" append-to-body>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form :model="form" label-width="80px">
              <el-form-item label="建筑物选择" :required="true" label-width="100px">
                <el-select
                  v-model="form.building_name"
                  placeholder="请选择建筑物"
                  clearable
                  size="small"
                  style="width: 300px"
                >
                  <el-option
                    v-for="dict in Name_result"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="所在楼层" :required="true" label-width="100px">
                <el-input v-model="form.y_floor" placeholder="如：1楼输入1楼" style="width: 200px"/>
              </el-form-item>
              <el-form-item label="平面图" label-width="100px">
                <el-button type="primary" size="mini" plain @click="planfile">选取文件</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="submit" @click="submitAdd">确 定</el-button>
        <el-button type="primary" v-if="protect" @click="protectAdd">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listPlaceFloor,buildingName,AddPlaceFloor,UpdPlaceFloor,deletePfr,FloorPlaceFloor} from "@/api/yunze/install/PlaceFloor";
import tools from "@/utils/yunze/tools";

export default {
  name: "index",
  data() {
    return {

      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      tools: tools,

      selTime: '',//时间选择
      mainwidth: 24,//宽度
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 用户表格数据
      cardList: null,
      // 是否显示弹出层
      open: false,
      // 时间数组
      timetypeInstall:[],
      // 查询条件
      typePlaceFloor: [],
      Name_result: [],//建筑物选择
      //执行修改
      formElse: {},
      // 表单参数
      form: {
        building_name:null,
        create_time: tools.gitData(),
      },
      submit: false,//确定
      protect: false,//保存
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 列信息
      columns: [
        {key: 1, label: `序号`, visible: true},
        {key: 2, label: `楼层`, visible: true},
        {key: 3, label: `安装点数量`, visible: true},
        {key: 4, label: `建筑物名称`, visible: true},
        {key: 5, label: `平面图`, visible: true},
        {key: 6, label: `创建时间`, visible: true},
      ],
      // 表单校验
      rules: {}
    };
  },

  created() {

    const id = this.$route.params.id;
    console.log(id)
    if (tools.Is_null(id)){
      this.queryParams.building_name = Number(id);
    }

    //加载 查询条件
    if (window['typePlaceFloor'] != undefined && window['typePlaceFloor'] != null && window['typePlaceFloor'] != '') {
      this.typePlaceFloor = window['typePlaceFloor'];
    } else {
      this.getDicts("yunze_typePlaceFloor").then(response => {
        window['typePlaceFloor'] = response.data;
        this.typePlaceFloor = window['typePlaceFloor'];
      });
    }


    //加载 时间赛选查询条件
    if (window['timetypeInstall'] != undefined && window['timetypeInstall'] != null && window['timetypeInstall'] != '') {
      this.timetypeInstall = window['timetypeInstall'];
    } else {
      this.getDicts("yunze_timetypeInstall").then(response => {
        window['timetypeInstall'] = response.data;
        this.timetypeInstall = window['timetypeInstall'];
      });
    }

    //查询建筑物名称  Name_result
    if (window['Name_result'] != undefined && window['Name_result'] != null && window['Name_result'] != '') {
      this.Name_result = window['Name_result'];
    } else {
      buildingName().then(response => {
        let jsonobj = JSON.parse(tools.Decrypt(response));
        window['Name_result'] = jsonobj.Data;
        // console.log(jsonobj.Data)
        this.Name_result = window['Name_result'];
      });
    }


    this.getList();
  },
  methods: {

    /**修改*/
    handleUpdate(row){
      // console.log(row)
      this.open = true;
      this.submit = false;
      this.protect = true;
      this.formElse.y_floor = row.y_floor;//传固定参数
      this.form = row;
    },
    /**删除*/
    handleDelete(row){
      let map = {};
      map.id = row.id;
      let Pwd_Str = tools.encrypt(JSON.stringify(map));
      tools.openAsk(this, 'warning', "删除不可恢复，还要继续吗?", this.deleteID, Pwd_Str);
    },
    deleteID(Pwd_Str) {
      deletePfr(Pwd_Str).then(response => {
        let jsonobj = JSON.parse(tools.Decrypt(response));
        // console.log(jsonobj);
        if (jsonobj.code == 200) {
          this.getList();
          this.msgSuccess(jsonobj.msg);
        } else {
          let msg = jsonobj.Data != null && jsonobj.Data != '' ? jsonobj.Data : jsonobj.msg;
          this.msgError(msg);
        }
      })
    },


    /**保存确定按钮*/
    protectAdd(){
      let _this = this;
      if (tools.VerificationsText(_this, _this.form.building_name, "建筑物名称不能为空！") == true &&
        tools.VerificationsText(_this, _this.form.y_floor, "所在楼层不能为空！") == true){

        if (this.formElse.y_floor === this.form.y_floor) {
          let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
          UpdPlaceFloor(Pwd_Str).then(response => {
            let jsonobj = JSON.parse(tools.Decrypt(response));
            //console.log(jsonobj);
            if (jsonobj.code == 200) {
              this.msgSuccess(jsonobj.msg);
              this.open = false;
              this.handleQuery();
            } else {
              this.msgError("获取数据异常，请联系管理员！");
            }
          });
        }else {
          let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
          FloorPlaceFloor(Pwd_Str).then(response => {
            let jsonobj = JSON.parse(tools.Decrypt(response));
            if (jsonobj.code != 200){
              UpdPlaceFloor(Pwd_Str).then(response => {
                let jsonobj = JSON.parse(tools.Decrypt(response));
                //console.log(jsonobj);
                if (jsonobj.code == 200) {
                  this.msgSuccess(jsonobj.msg);
                  this.open = false;
                  this.handleQuery();
                } else {
                  this.msgError("获取数据异常，请联系管理员！");
                }
              });
            }else {
              this.msgError("楼层已存在！");
            }
          });
        }

      }
    },

    /**新增按钮*/
    handleAdd(row){
      this.open = true;
      this.submit = true;
      this.protect = false;
      this.form.create_time=tools.gitData();
    },
    /**新增确定按钮*/
    submitAdd(){
      let _this = this;
      if (tools.VerificationsText(_this, _this.form.building_name, "建筑物名称不能为空！") == true &&
        tools.VerificationsText(_this, _this.form.y_floor, "所在楼层不能为空！") == true
      ){
        let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
        FloorPlaceFloor(Pwd_Str).then(response => {
          let jsonobj = JSON.parse(tools.Decrypt(response));
          if (jsonobj.code != 200){
            let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
            AddPlaceFloor(Pwd_Str).then(response => {
              let jsonobj = JSON.parse(tools.Decrypt(response));
              //console.log(jsonobj);
              if (jsonobj.code == 200) {
                this.msgSuccess(jsonobj.msg);
                this.open = false;
                this.handleQuery();
              } else {
                this.msgError("获取数据异常，请联系管理员！");
              }
            });
          }else {
            this.msgError("楼层已存在！");
          }
        });

      }
    },

    /**平面图*/
    planfile(){

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /*获取查询参数*/
    getParams() {

      if (this.$refs.dept != undefined & this.$refs.dept != null & this.$refs.dept != 'undefined') {
        //console.log(this.$refs.dept.getCheckedKeys());
        if (this.$refs.dept.getCheckedKeys().length > 0) {
          this.queryParams.agent_id = this.$refs.dept.getCheckedKeys();
        }
      }
      if (tools.Is_null(this.queryParams.timetype) && this.selTime != null) {
        //console.log(this.selTime);
        this.queryParams.staTime = this.selTime[0];
        this.queryParams.endTime = this.selTime[1];
      }


    },


    /** 查询执行日志列表 */
    getList() {

      this.loading = true;
      this.getParams();
      this.queryParams.page = 1;
      //console.log(this.queryParams);
      let Pwd_Str = tools.encrypt(JSON.stringify(this.queryParams));
      //console.log(Pwd_Str);
      listPlaceFloor(Pwd_Str).then(response => {
          let jsonobj = JSON.parse(tools.Decrypt(response));
          // console.log(jsonobj);
          if (jsonobj.code == 200) {
            this.cardList = jsonobj.Data.Data;
            this.total = jsonobj.Data.Pu.rowCount;
          } else {
            this.msgError("获取数据异常，请联系管理员！");
          }
          this.loading = false;
        }
      );
    },


    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {};
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.selTime = null;
      this.handleQuery();
    },


  }
};
</script>

<style scoped>


</style>

